package com.yisu.es.executor;

import com.yisu.es.model.EsQueries;
import com.yisu.es.model.EsQuery;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Map;

/**
 * @className EsQueryExecutor
 * @description 执行查询
 */
@Slf4j
public class EsQueryExecutorBak {
    private final RestHighLevelClient client;
    private final EsQueries esQueries;

    public EsQueryExecutorBak(RestHighLevelClient client, EsQueries esQueries) {
        this.client = client;
        this.esQueries = esQueries;
    }

    /**
     * 执行查询请求
     *
     * @param queryId 查询的唯一标识符
     * @param params  查询参数
     * @return 查询结果
     * @throws IOException 如果发生IO异常
     * @throws IllegalArgumentException 如果未找到指定的查询
     */
    public SearchResponse executeQuery(String queryId, Map<String, Object> params) throws IOException {
        EsQuery esQuery = esQueries.getQueries().stream()
                .filter(q -> q.getId().equals(queryId))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("Query not found"));

//        String queryString = esQuery.getQuery();
//        // 替换占位符
//        for (Map.Entry<String, Object> entry : params.entrySet()) {
//            queryString = queryString.replace("${" + entry.getKey() + "}", entry.getValue().toString());
//        }

        String queryString = replacePlaceholders(esQuery.getQuery(), params);
        log.info("Executing query with ID: {}, Query: {}, Params: {}", queryId, esQuery.getQuery(), params);

        SearchRequest searchRequest = new SearchRequest("tel_user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.wrapperQuery(queryString.trim()));
        searchRequest.source(sourceBuilder);

        return client.search(searchRequest, RequestOptions.DEFAULT);
    }




    private String replacePlaceholders(String template, Map<String, Object> params) {
        return params.entrySet().stream()
                .reduce(template, (acc, entry) -> acc.replace("${" + entry.getKey() + "}", String.valueOf(entry.getValue())),
                        (left, right) -> left);
    }



    /**
     * 执行精确匹配查询
     *
     * @return 查询结果响应对象
     * @throws IOException 如果在执行查询过程中发生I/O异常
     */
    public SearchResponse executeExactMatchQuery() throws IOException {
        // 构建精确匹配查询
        SearchRequest searchRequest = new SearchRequest("tel_user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // 使用 term 查询进行精确匹配
        sourceBuilder.query(QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("user_name.keyword", "李四")));

        sourceBuilder.from(0);
        sourceBuilder.size(1);
        // 确保返回所有字段
        sourceBuilder.fetchSource(true);
        // 打印实际的查询字符串
        log.info("Search Request: {}", sourceBuilder.toString());
        searchRequest.source(sourceBuilder);

        // 执行查询
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        log.info("Query executed successfully");

        return response;
    }

}

